package com.sam4cloud.common.core.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.sam4cloud.common.core.cons.HttpCode;
import com.sam4cloud.common.core.exception.ApiException;

import java.util.Collection;

/**
 * @Author: Sam
 * @Date: 2022/11/10 11:30
 * @Description: 断言
 */
public class AssertUtil {

    /**
     * 默认错误码 500
     */
    private final static int DEFAULT_CODE = HttpCode.SC_INTERNAL_SERVER_ERROR;

    private AssertUtil() {
    }

    public static void failure(String msg) {
        failure(DEFAULT_CODE, msg);
    }

    public static void failure(String msg, Throwable cause) {
        failure(DEFAULT_CODE, msg, cause);
    }

    public static void failure(int code, String msg) {
        throw new ApiException(code, msg);
    }

    public static void failure(int code, String msg, Throwable cause) {
        throw new ApiException(code, msg, cause);
    }


    public static void isFalse(boolean expression, String msg) throws ApiException {
        isFalse(expression, DEFAULT_CODE, msg);
    }

    public static void isFalse(boolean expression, int code, String msg) throws ApiException {
        if (!expression) {
            failure(code, msg);
        }
    }

    public static void isTrue(boolean expression, String msg) throws ApiException {
        isTrue(expression, DEFAULT_CODE, msg);
    }

    public static void isTrue(boolean expression, int code, String msg) throws ApiException {
        if (expression) {
            failure(code, msg);
        }
    }

    public static void isNull(Object obj, String msg) throws ApiException {
        isNull(obj, DEFAULT_CODE, msg);
    }

    public static void isNull(Object obj, int code, String msg) throws ApiException {
        if (obj == null) {
            failure(code, msg);
        }
    }

    public static void isNotNull(Object obj, String msg) throws ApiException {
        isNotNull(obj, DEFAULT_CODE, msg);
    }

    public static void isNotNull(Object obj, int code, String msg) throws ApiException {
        if (obj != null) {
            failure(code, msg);
        }
    }

    public static void isNotEmpty(Collection<?> collection, String msg) throws ApiException {
        isNotEmpty(collection, DEFAULT_CODE, msg);
    }

    public static void isNotEmpty(Collection<?> collection, int code, String msg) throws ApiException {
        if (CollUtil.isNotEmpty(collection)) {
            failure(code, msg);
        }
    }

    public static void isEmpty(Collection<?> collection, String msg) throws ApiException {
        isEmpty(collection, DEFAULT_CODE, msg);
    }

    public static void isEmpty(Collection<?> collection, int code, String msg) throws ApiException {
        if (CollUtil.isEmpty(collection)) {
            failure(code, msg);
        }
    }

    public static void isNotEmpty(String string, String msg) throws ApiException {
        isNotEmpty(string, DEFAULT_CODE, msg);
    }

    public static void isNotEmpty(String string, int code, String msg) throws ApiException {
        if (StrUtil.isNotEmpty(string)) {
            failure(code, msg);
        }
    }

    public static void isEmpty(String string, String msg) throws ApiException {
        isEmpty(string, DEFAULT_CODE, msg);
    }

    public static void isEmpty(String string, int code, String msg) throws ApiException {
        if (StrUtil.isEmpty(string)) {
            failure(code, msg);
        }
    }

    public static void isEqStr(String source, String target, String msg) throws ApiException {
        isEqStr(source, target, DEFAULT_CODE, msg);
    }

    public static void isEqStr(String source, String target, int code, String msg) throws ApiException {
        if (source.equals(target)) {
            failure(code, msg);
        }
    }

    public static void isNotEqStr(String source, String target, String msg) throws ApiException {
        isNotEqStr(source, target, DEFAULT_CODE, msg);
    }

    public static void isNotEqStr(String source, String target, int code, String msg) throws ApiException {
        if (!source.equals(target)) {
            failure(code, msg);
        }
    }
}